package com.wanjia.admin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanjia.admin.server.config.security.JwtTokenUtil;
import com.wanjia.admin.server.mapper.AdminMapper;
import com.wanjia.admin.server.pojo.Admin;
import com.wanjia.admin.server.service.AdminService;
import com.wanjia.project.commons.dto.login.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private UserDetailsService userDetailsService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private AdminMapper adminMapper;
    /**
     * 登录之后返回Token
     * @param account
     * @param password
     * @param code
     * @param request
     * @return
     */
    @Override
    public RespBean login(String account, String password, String code, HttpServletRequest request) {
        /*// 验证码
        String captcha = (String) request.getSession().getAttribute("captcha");
        // 判断验证码
        if ("".equals(code) || !captcha.equalsIgnoreCase(code)) {
            return RespBean.error("验证码输入有误，请重新输入！");
        }*/
        // 获取UserDetails
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_account", account);
        queryWrapper.eq("admin_pwd", password);
        Admin admin = adminMapper.selectOne(queryWrapper);
        // 判断用户是否被禁用
        if (admin == null) {
            System.out.println("没有查到该用户！！");
            return null;
        } else {
            if (admin.isEnabled()) {
                // 前端获取的密码通过passwordEncoder与数据库中的密码对比
                if (admin != null) {
                    // 更新security登录用户对象
                    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(admin, null);
                    // 将authenticationToken放入spring security全局中
                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                    // 生成Token
                    String token = jwtTokenUtil.generateToken(admin);
                    // 将token和头部信息存入map中，登录成功后带给前端。
                    HashMap<String, String> tokenMap = new HashMap<>();
                    tokenMap.put("token", token);
                    return RespBean.success("登录成功", tokenMap);
                }
                return RespBean.error("用户名或密码错误");
            }
            return RespBean.error("该账号也被禁用,请联系管理员!");
        }
    }
    /**
     * 根据编号获取用户
     * @param account
     * @return
     */
    @Override
    public Admin getAdminByUserName(String account) {
        // 用户名、启用状态
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("admin_account", account));
    }

}
